<?php

namespace App\Logic\Friend;

use App\Logic\Logic;
use App\Model\Friend\FriendBean;
use App\Model\Friend\FriendModel;
use App\Model\Message\MessageBean;
use App\Model\User\UserBean;
use App\Model\User\UserModel;
use App\Msg\MsgCode;
use App\Server\Task\AddFriendTask;
use App\Server\Task\PassFriendTask;
use App\Server\Task\PushMessageTask;
use App\Utility\Common;
use App\Utility\Validator;
use EasySwoole\Core\Http\Message\Status;
use EasySwoole\Core\Swoole\Task\TaskManager;
use EasySwoole\Core\Utility\Random;

class FriendLogic extends Logic {
	function addFriend(UserBean $myinfo, $fuid) {
		if ($myinfo->getUid () == $fuid) {
			$this->setMsg ( MsgCode::NOT_ADD_SELF_FRIEND, "friend" );
		}
		$FriendInfo = (new UserModel ())->getUserByUid ( $fuid );
		if (! Validator::isEmpty ( $FriendInfo->getUid () )) {
			$this->setMsg ( MsgCode::FROEND_NOT_EXIST, "friend" );
		}
		
		// 确定一下账号已经是好友
		$isfriend = (new FriendModel ())->isFriend ( $fuid, $myinfo->getUid () );
		if (! empty ( $isfriend->getId () )) {
			$this->setMsg ( MsgCode::NOT_REPEAT_ADD_FRIEND, "friend" );
		}
		// 确定申请是不是已经添加了
		$log = (new FriendModel ())->getAddLog ( $fuid, $myinfo->getUid () );
		if (! empty ( $log->getId () )) {
			$this->setMsg ( MsgCode::ADD_FRIEND_LOG_EXIST, "friend" );
		}
		// 添加一个好友申请
		$friendBean = new FriendBean ();
		$friendBean->setAddtime ( time () );
		$friendBean->setFuid ( $fuid );
		$friendBean->setMuid ( $myinfo->getUid () );
		$friendBean->setId ( Random::randStr ( 11 ) );
		$m = new FriendModel ();
		$flag = $m->addUser ( $friendBean );
		if (! $flag) {
			(new Common ())->E ( $m->getLastError () );
			$this->setMsg ( MsgCode::FRIEND_DB_ERROR, "friend" );
		}
		
		// 通过缓存获取好友在线的fd
		$tofd = (new UserModel ())->getFdByUid ( $fuid );
		$myfd = (new UserModel ())->getFdByUid ( $myinfo->getUid () );
		(new Common ())->E ( "get tofd = " . $tofd . " by fuid" );
		TaskManager::async ( (new AddFriendTask ( [ 
				'userinfo' => $myinfo->toArray (),
				"friendinfo" => $FriendInfo->toArray (),
				'tofd' => $tofd,
				'myfd' => $myfd,
				'id' => Random::randStr ( 3 ) . Random::randStr ( 3 ) . Random::randStr ( 5 ) 
		] )) );
	}
	function getFriendRequests(UserBean $userinfo): array {
		return (new FriendModel ())->getFriendRequests ( $userinfo->getUid () );
	}
	function dealFriendRequest(UserBean $userinfo, $id, $status) {
		if ($status == FriendBean::ADD_FRIEND_ING) {
			$this->setMsg ( MsgCode::OPERAT_FAIL, "common" );
		}
		$friend = (new FriendModel ())->getInfoById ( $id );
		if ($friend->getIspass () != 1) {
			$this->setMsg ( MsgCode::OPERAT_FAIL, "common" );
		}
		$f = new FriendBean ();
		$f->setId ( $id );
		$f->setIspass ( $status );
		$re = (new FriendModel ())->updateDataById ( $f );
		
		// 通过缓存获取好友在线的fd
		$tofd = (new UserModel ())->getFdByUid ( $friend->getMuid () );
		$myfd = (new UserModel ())->getFdByUid ( $userinfo->getUid () );
		$finendinfo = (new UserModel ())->getUserByUid ( $friend->getMuid () );
		if ($re) {
			TaskManager::async ( (new PassFriendTask ( [ 
					"tofd" => $tofd,
					"myfd" => $myfd,
					"status" => $status,
					"userinfo" => $userinfo->objToArray ( true ),
					"friendinfo" => $finendinfo->objToArray ( true ),
					'id' => Random::randStr ( 3 ) . Random::randStr ( 3 ) . Random::randStr ( 5 ) 
			] )) );
			return true;
		} else {
			$this->setMsg ( MsgCode::FRIEND_DB_ERROR, "friend" );
		}
	}
	function getFriendList(UserBean $userinfo): array {
		return (new FriendModel ())->getFriends ( $userinfo->getUid () );
	}
	function PushMessageToFriend(UserBean $userinfo, $message, $fuid): void {
		$FriendInfo = (new UserModel ())->getUserByUid ( $fuid );
		if (! Validator::isEmpty ( $FriendInfo->getUid () )) {
			$this->setMsg ( MsgCode::FROEND_NOT_EXIST, "friend" );
		}
		
		$isfriend = (new FriendModel ())->isFriend ( $fuid, $userinfo->getUid () );
		if (empty ( $isfriend->getId () )) {
			$this->setMsg ( MsgCode::NOT_FRIEND, "friend" );
		}
		
		$tofd = (new UserModel ())->getFdByUid ( $fuid );
		
		// if (! Validator::isEmpty($tofd)) {
		// $this->setMsg(MsgCode::FRIEND_NOT_ONLINE, "friend");
		// }
		if (! (new Common ())->isOnline ( $tofd )) {
			$this->setMsg ( MsgCode::FRIEND_NOT_ONLINE, "friend" );
		}
		$id = Random::randStr ( 3 ) . Random::randStr ( 3 ) . Random::randStr ( 5 );
		$mb = new MessageBean ();
		$mb->setId ( $id );
		$mb->setAddtime ( time () );
		$mb->setContent ( $message );
		$mb->setFromid ( $userinfo->getUid () );
		$mb->setUid ( $fuid );
		$mb->setType ( MessageBean::FRIEND_CHAT_TYPE );
		
		// $msg = (new Common ())->dealMessage ( MsgCode::MESSAGE_SUCCESS, $mb, MsgInfo::SYSTEM_MSG_TITLE );
		// (new Common())->pushMessage($tofd, $msg);
		$finendinfo = (new UserModel ())->getUserByUid ( $fuid );
		TaskManager::async ( (new PushMessageTask ( [ 
				"userinfo" => $userinfo->objToArray ( true ),
				"friendinfo" => $finendinfo->objToArray ( true ),
				'msg' => $mb->objToArray ( true ),
				'tofd' => $tofd 
		] )) );
	}
}